<?php

/**
 * Created by IntelliJ IDEA.
 * User: fangzhenyi
 * Date: 16/2/12
 * Time: 下午5:45
 */
class DB
{
    public $mydb;

    function __construct()
    {
        $this->mydb =  new PDO("mysql:dbname=sleep1;host=127.0.0.1;charset=utf8","root", "520520fzy");
        $this->mydb->exec("set names 'utf8'");
    }


    public function saveData($sql)
    {
        $stmt = $this->mydb->prepare($sql);
        $re = $stmt->execute();
        if ($re) return true;
        else {
            return false;
        }
    }

    public function deleteData($sql)
    {
        $stmt = $this->mydb->prepare($sql);
        $re = $stmt->execute();
        if ($re) return true;
        else return false;
    }

    public function updateData($sql)
    {
        $stmt = $this->mydb->prepare($sql);
        $re = $stmt->execute();
        if ($re) return true;
        else {
            echo mysqli_error();
            return false;
        }
    }

    public function queryResourceData($sql)
    {
        $stmt = $this->mydb->prepare($sql);
        $re = $stmt->execute();
        if ($re) {
            return $re;
        } else {
            echo mysqli_error();
            return false;
        }
    }

    public function queryMoreData($sql)
    {
        $stmt = $this->mydb->prepare($sql);
        $re = $stmt->execute();
        if ($re) {
            $result = array();
            while ($myResult = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
                array_push($result, $myResult);
            }
            return $result;
        } else {
            return false;
        }
    }

    public function queryOneData($sql)
    {
        $stmt = $this->mydb->prepare($sql);
        $re = $stmt->execute();
        if ($re) {
            $array = $stmt->fetch(PDO::FETCH_ASSOC);
            print_r($array);
            if ($array) {
                return $array;
            } else {
                return false;
            }
        } else {
            echo mysqli_error();
            return false;
        }
    }

    public function isExistData($sql)
    {
        $stmt = $this->mydb->prepare($sql);
        $re = $stmt->execute();
        if ($re) {
            $array = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($array) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


}


